package com.renzhichu.app.polyv;


import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.easefun.polyv.businesssdk.model.video.PolyvPlayBackVO;
import com.easefun.polyv.businesssdk.service.PolyvLoginManager;
import com.easefun.polyv.businesssdk.vodplayer.PolyvVodSDKClient;
import com.easefun.polyv.cloudclass.config.PolyvLiveSDKClient;
import com.easefun.polyv.cloudclass.model.PolyvLiveStatusVO;
import com.easefun.polyv.cloudclass.net.PolyvApiManager;
import com.easefun.polyv.cloudclassdemo.login.PolyvCloudClassLoginActivity;
import com.easefun.polyv.cloudclassdemo.watch.PolyvCloudClassHomeActivity;
import com.easefun.polyv.foundationsdk.log.PolyvCommonLog;
import com.easefun.polyv.foundationsdk.net.PolyvResponseBean;
import com.easefun.polyv.foundationsdk.net.PolyvResponseExcutor;
import com.easefun.polyv.foundationsdk.net.PolyvrResponseCallback;
import com.easefun.polyv.linkmic.PolyvLinkMicClient;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.renzhichu.app.R;

import java.io.IOException;

import javax.annotation.Nonnull;

import io.reactivex.disposables.Disposable;
import retrofit2.adapter.rxjava2.HttpException;


public class PolyvLiveModules extends ReactContextBaseJavaModule {

    // <editor-fold defaultstate="collapsed" desc="成员变量">
    private Disposable getTokenDisposable,verifyDispose;
    private static final String TAG = "PolyvCloudClassLoginAct";
    // </editor-fold>
    public PolyvLiveModules(@Nonnull ReactApplicationContext reactContext) {
        super(reactContext);
        System.out.println("createViewInstancecreateViewInstance");
    }


    @Nonnull
    @Override
    public String getName() {
        return "RNTPolyvLiveManager";
    }

    // <editor-fold defaultstate="collapsed" desc="登录处理">
    @ReactMethod
    public void login(String userId, String appSecert, String channelId, String appId) {
        checkToken(userId, appSecert, channelId, null, appId);
    }


    @ReactMethod
    public void goLogin(){
        this.getCurrentActivity().startActivity(new Intent(this.getCurrentActivity(), PolyvCloudClassLoginActivity.class));
    }

    // <editor-fold defaultstate="collapsed" desc="登录处理">


    private void checkToken(String userId, String appSecret, String channel, String vid, String appId) {
        //请求token接口
        getTokenDisposable = PolyvLoginManager.checkLoginToken(userId, appSecret, appId,
                channel, vid,
                new PolyvrResponseCallback<PolyvResponseBean>() {
                    @Override
                    public void onSuccess(PolyvResponseBean responseBean) {
                        PolyvLinkMicClient.getInstance().setAppIdSecret(appId, appSecret);
                        PolyvLiveSDKClient.getInstance().setAppIdSecret(appId, appSecret);
                        PolyvVodSDKClient.getInstance().initConfig(appId, appSecret);
                        requestLiveStatus(userId,channel);

                    }

                    @Override
                    public void onFailure(PolyvResponseBean<PolyvResponseBean> responseBean) {
                        super.onFailure(responseBean);
                        failedStatus(responseBean.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                        errorStatus(e);
                    }
                });
    }

    public void failedStatus(String message) {
        ToastUtils.showLong(message);
    }

    public void errorStatus(Throwable e) {
        PolyvCommonLog.exception(e);
        if (e instanceof HttpException) {
            try {
                ToastUtils.showLong(((HttpException) e).response().errorBody().string());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        } else {
            ToastUtils.showLong(e.getMessage());
        }
    }

    private void requestLiveStatus(String userId,String channelId) {
        verifyDispose = PolyvResponseExcutor.excuteUndefinData(PolyvApiManager.getPolyvLiveStatusApi().geLiveStatusJson(channelId)
                , new PolyvrResponseCallback<PolyvLiveStatusVO>() {
                    @Override
                    public void onSuccess(PolyvLiveStatusVO statusVO) {
                        String data = statusVO.getData();
                        String[] dataArr = data.split(",");

                        boolean isAlone = "alone".equals(dataArr[1]);//是否有ppt

                        startActivityForLive(userId, isAlone,channelId);
                    }

                    @Override
                    public void onFailure(PolyvResponseBean<PolyvLiveStatusVO> responseBean) {
                        super.onFailure(responseBean);
                        failedStatus(responseBean.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        errorStatus(e);
                    }
                });
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="startActivity">
    private void startActivityForLive(String userId, boolean isAlone,String channelId) {
        PolyvCloudClassHomeActivity.startActivityForLive(this.getCurrentActivity(), channelId, userId, isAlone);
    }

    // </editor-fold>


}
